package com.example.demo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.View;


/**
 * Created by sunmo on 2016/12/4.
 */
public class ForegroundView extends View {
    private Paint mFirPaint;
    private Paint mSecPaint;
    private Paint mThiPaint;
    private Paint mTxtPaint;//文字画笔

    private float mCenterX;
    private float mCenterY;

    private Ball mFirBall;
    private Ball mSecBall;
    private Ball mThiBall;

    private static final int DEFAULT_RADIUS = 10;
    private static int $horGap;
    private static int $verGap;
    private static float yScale;
    private static final double PI = 3.1415;
    private static final int ANIM_TIME = 1 * 1000;
    private static final int ANIM_RADIUM = 15;//三个球公转半径

    public static final String COLOR_DEFAULT = "#999999";
    public static final String COLOR_SELECTED = "#ffffff";


    //三个球的起点位置
    private float firX0;
    private float firY0;
    private float secX0;
    private float secY0;
    private float thiX0;
    private float thiY0;

    //文字颜色
    private int[] txtColors = new int[4];
    private AnimatorSet animSet;

    private GestureDetector mDetector;

    public ForegroundView(Context context) {
        super(context);
    }

    public ForegroundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        $horGap = getResources().getDimensionPixelSize(R.dimen.hor_gap);
        $verGap = getResources().getDimensionPixelSize(R.dimen.ver_gap);
        yScale = 0.7f;
        mTxtPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTxtPaint.setStyle(Paint.Style.FILL);
        mTxtPaint.setStrokeWidth(2);
        mTxtPaint.setTextSize(30);
        for (int i = 0; i < txtColors.length; i++) {
            txtColors[i] = Color.parseColor(COLOR_DEFAULT);
        }
        txtColors[0] = Color.parseColor(COLOR_SELECTED);
        //定义三个球
        mFirBall = new Ball();
        mSecBall = new Ball();
        mThiBall = new Ball();
        mFirBall.setRadius(DEFAULT_RADIUS);
        mSecBall.setRadius(DEFAULT_RADIUS + 2);
        mThiBall.setRadius(DEFAULT_RADIUS);
        //初始化画球的笔
        mFirPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFirPaint.setColor(Color.BLACK);
        mSecPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSecPaint.setColor(Color.BLACK);
        mThiPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mThiPaint.setColor(Color.BLACK);

        //获取屏幕中心
        mCenterX = MetricsUtils.winMetrics(context).widthPixels / 2;
        mCenterY = MetricsUtils.winMetrics(context).heightPixels / 2;
        LogUtils.e("w/2=" + mCenterX + " :h/2=" + mCenterY);

        //球的起点位置
        firX0 = mCenterX - $horGap;
        secX0 = mCenterX;
        thiX0 = mCenterX + $horGap;
        firY0 = (mCenterY - $verGap);
        secY0 = (mCenterY - $verGap - 20);
        thiY0 = (mCenterY - $verGap);
        mFirBall.setCenterX(firX0);
        mSecBall.setCenterX(secX0);
        mThiBall.setCenterX(thiX0);
        mFirBall.setCenterY(firY0);
        mSecBall.setCenterY(secY0);
        mThiBall.setCenterY(thiY0);

    }


    /**
     * 对外提供设置动画的方法
     *
     * @param angle 为摆动的最大圆心角
     */
    public void initAnimation(final float angle) {
        //球自转动画
        ObjectAnimator mFirAnim = ObjectAnimator.ofFloat(mFirBall, "rotation", 0f, 60f, 0f, 30f, 0f);
        mFirAnim.setDuration(ANIM_TIME);
        //球公转动画
        ValueAnimator mFirAxis = ValueAnimator.ofFloat(0f, (float) (angle / 180 * PI), 0f, -(float) (angle / 360 * PI), 0);
        mFirAxis.setDuration(ANIM_TIME);
        mFirAxis.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //计算球心坐标
                float i = (float) animation.getAnimatedValue();
                float cX = (float) (firX0 + Math.sin(i) * ANIM_RADIUM);
                float cY = (float) (firY0 - (1 - Math.cos(i)) * ANIM_RADIUM);
                mFirBall.setCenterX(cX);
                mFirBall.setCenterY(cY);
                //LogUtils.e("i=" + i + " :cX=" + cX + " :cY=" + cY);
                invalidate();
            }
        });
        ObjectAnimator mSecAnim = ObjectAnimator.ofFloat(mFirBall, "rotation", 0f, 60f, 0f, -30f, 0f);
        mSecAnim.setDuration(ANIM_TIME);
        ValueAnimator mSecAxis = ValueAnimator.ofFloat(0f, (float) (angle / 180 * PI), 0f, -(float) (angle / 360 * PI), 0);
        mSecAxis.setDuration(ANIM_TIME);
        mSecAxis.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float i = (float) animation.getAnimatedValue();
                float cX = (float) (secX0 + Math.sin(i) * ANIM_RADIUM);
                float cY = (float) (secY0 - (1 - Math.cos(i)) * ANIM_RADIUM);
                mSecBall.setCenterX(cX);
                mSecBall.setCenterY(cY);
//                LogUtils.e("i=" + i + " :cX=" + cX + " :cY=" + cY);
                invalidate();
            }
        });

        ObjectAnimator mThiAnim = ObjectAnimator.ofFloat(mFirBall, "rotation", 0f, 60f, 30f, 0f);
        mThiAnim.setDuration(ANIM_TIME);
        ValueAnimator mThiAxis = ValueAnimator.ofFloat(0f, (float) (angle / 180 * PI), 0f, -(float) (angle / 360 * PI), 0);
        mThiAxis.setDuration(ANIM_TIME);
        mThiAxis.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float i = (float) animation.getAnimatedValue();
                float cX = (float) (thiX0 + Math.sin(i) * ANIM_RADIUM);
                float cY = (float) (thiY0 - (1 - Math.cos(i)) * ANIM_RADIUM);
                mThiBall.setCenterX(cX);
                mThiBall.setCenterY(cY);
//                LogUtils.e("i=" + i + " :cX=" + cX + " :cY=" + cY);
                invalidate();
            }
        });
        animSet = new AnimatorSet();
        animSet.playTogether(mFirAnim, mFirAxis, mSecAnim, mSecAxis, mThiAnim, mThiAxis);
        animSet.start();
    }

    public ForegroundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ForegroundView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //处理文字；
        mTxtPaint.setColor(txtColors[0]);
//        canvas.drawText("Hello", mCenterX, mCenterY, mTxtPaint);
        LogUtils.e("mCenterX=" + mCenterX + " mY=" + mCenterY);
        canvas.drawText("A", (float) (mCenterX * (1 - 0.5)), (float) ((mCenterY - $verGap) * yScale), mTxtPaint);
        mTxtPaint.setColor(txtColors[1]);
        canvas.drawText("B", (float) (mCenterX * (1 - 0.17)), (float) ((mCenterY - $verGap) * (yScale - 0.08)), mTxtPaint);
        mTxtPaint.setColor(txtColors[2]);
        canvas.drawText("C", (float) (mCenterX * (1 + 0.17)), (float) ((mCenterY - $verGap) * (yScale - 0.08)), mTxtPaint);
        mTxtPaint.setColor(txtColors[3]);
        canvas.drawText("D", (float) (mCenterX * (1 + 0.5)), (float) ((mCenterY - $verGap) * yScale), mTxtPaint);

        /**
         * 画圆
         */
        canvas.drawCircle(mFirBall.getCenterX(), mFirBall.getCenterY(), mFirBall.getRadius(), mFirPaint);
        canvas.drawCircle(mSecBall.getCenterX(), mSecBall.getCenterY(), mSecBall.getRadius(), mSecPaint);
        canvas.drawCircle(mThiBall.getCenterX(), mThiBall.getCenterY(), mThiBall.getRadius(), mThiPaint);
    }

    //set text color

    /**
     * @param position 当前项位置
     * @param step     移动的跨度，左负右正
     */
    public synchronized void setSelectedTxtColor(int position, int step) {
        txtColors[position] = Color.parseColor(COLOR_SELECTED);
        if (step > 0) {
            txtColors[position - step] = Color.parseColor(COLOR_DEFAULT);
        } else {
            txtColors[position + step] = Color.parseColor(COLOR_DEFAULT);
        }
        postInvalidate();
    }

    public synchronized void setSelectedTxtColor(int position) {
        for (int i = 0; i < txtColors.length; i++) {
            txtColors[i] = Color.parseColor(COLOR_DEFAULT);
        }
        txtColors[position] = Color.parseColor(COLOR_SELECTED);
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //setCenter(w, h);
    }

    private void setCenter(int w, int h) {
        //横屏
        mCenterX = h / 2;
        mCenterY = w / 2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //setCenter(getWidth(), getHeight());
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        LogUtils.e("onTouch");
//        mDetector.onTouchEvent(event);
//        return true;
//    }

    /**
     * ball class
     */
    private class Ball {
        private float radius;
        private float centerX;
        private float centerY;
        private int color;

        public float getCenterY() {
            return centerY;
        }

        public void setCenterY(float centerY) {
            this.centerY = centerY;
        }

        public float getRadius() {
            return radius;
        }

        public void setRadius(float radius) {
            this.radius = radius;
        }

        public float getCenterX() {
            return centerX;
        }

        public void setCenterX(float centerX) {
            this.centerX = centerX;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }
    }
}
